/**
 * @Author: LiMeng
 * @Date: 2017/6/5
 */
var add_fireLicense_func = function () {
    WindowManager.openLayer({
        type: 2,
        title: '添加用火作业许可信息',
        area: ['1150px', '70%'],
        content: ROOT_PATH + '/workLicense/fireLicense/add.shtml',
        callBack: function (data) {
            if (data) {
                $('#dg1').datagrid('load');
            }
        }
    });
};
var edit_fireLicense_func = function () {
    var $selected = $('#dg1').datagrid('getSelections');
    if ($selected.length !== 1) {
        layer.alert('请选择一个用火作业许可信息！');
        return false;
    }
    WindowManager.openLayer({
        type: 2,
        title: '编辑用火作业许可信息',
        area: ['1150px', '70%'],
        content: ROOT_PATH + '/workLicense/fireLicense/edit.shtml?id=' + $selected[0].id,
        callBack: function (data) {
            if (data) {
                $('#dg1').datagrid('load');
            }
        }
    });
};
var delete_fireLicense_func = function () {
    var $selected = $('#dg1').datagrid('getSelections');
    if ($selected.length !== 1) {
        layer.alert('请选择一个用火作业许可信息！');
        return false;
    }
    var index = parent.layer.confirm('是否确定删除？', function () {
        $.ajax({
            type: "POST",
            url: ROOT_PATH + '/workLicense/fireLicense/delete.shtml',
            data: {id: $selected[0].id},
            success: function (result) {
                result = $.parseJSON(result);
                if (result.success) {
                    parent.layer.msg(result.message, {time: 1000, icon: 1}, function () {
                        $('#dg1').datagrid('load');
                        parent.layer.close(index); //先得到当前iframe层的索引
                    });
                } else {
                    parent.layer.msg(result.message);
                }
            }
        });
    });
};
var view_fireLicense_func = function (id) {
    WindowManager.openLayer({
        type: 2,
        title: '查看用火作业许可信息',
        area: ['1150px', '70%'],
        content: ROOT_PATH + '/workLicense/fireLicense/view.shtml?id=' + id
    });
};
var copy_fireLicense_func = function () {
    var $selected = $('#dg1').datagrid('getSelections');
    if ($selected.length !== 1) {
        layer.alert('请选择一个用火作业许可信息！');
        return false;
    }
    WindowManager.openLayer({
        type: 2,
        title: '新增用火作业许可信息',
        area: ['1150px', '70%'],
        content: ROOT_PATH + '/workLicense/fireLicense/copy.shtml?id=' + $selected[0].id,
        callBack: function (data) {
            if (data) {
                $('#dg1').datagrid('load');
            }
        }
    });
};
var getSelectedAddUser = function (data) {
    var names = '';
    var ids = '';
    for (var index in data) {
        names += ',' + data[index].realName;
        ids += ',' + data[index].id;
    }

    $('#addUserName').textbox('setValue', names.substring(1));
    $('#addUserId').val(ids.substring(1));
};
var getSelectedApplyUser = function (data) {
    var names = '';
    var ids = '';
    for (var index in data) {
        names += ',' + data[index].realName;
        ids += ',' + data[index].id;
    }

    $('#applyUserName').textbox('setValue', names.substring(1));
    $('#applyUserId').val(ids.substring(1));
};
var openSelectUser = function (title, callback) {
    WindowManager.openLayer({
        type: 2,
        title: title,
        area: ['900px', '500px'],
        content: ROOT_PATH + '/permission/user/simpleList.shtml',
        callBack: callback
    });
};
$(function () {
    $('#fireLevelId').combobox({
        url: ROOT_PATH + '/permission/dictionary/queryDictionaryByDictionaryTypeCode.shtml?code=' + CONSTANT.DictionaryTypeDefConstants.WORK_LEVEL,
        valueField: 'id',
        editable: false
    });

    $('#applyDeptId').combotree({
        url: ROOT_PATH + '/permission/department/getDepartmentTree.shtml?parentId=0',
        editable: false
    });

    $('#applyUserName').textbox({
        icons: [{
            iconCls: 'icon-search',
            handler: function () {
                openSelectUser('选择申请人', getSelectedApplyUser);
            }
        }],
        editable: false
    });

    $('#addUserName').textbox({
        icons: [{
            iconCls: 'icon-search',
            handler: function () {
                openSelectUser('选择开票人', getSelectedAddUser);
            }
        }],
        editable: false
    });

    $('#addDeptId').combotree({
        url: ROOT_PATH + '/permission/department/getDepartmentTree.shtml?parentId=0',
        editable: false
    });
    //初始化加载datagrid
    $('#dg1').datagrid({
        method: 'post',
        url: ROOT_PATH + '/workLicense/fireLicense/list.shtml',
        queryParams: {
            sort: 'addTime',
            order: 'desc'
        },
        toolbar: '#toolbar',
        fitColumns: true,
        pagination: true,
        rownumbers: true,
        sortOrder: true,
        striped: true,
        fit: true,
        columns: [[
            {field: 'ckid', checkbox: true},
            {field: 'code', title: '票证编号', width: 60, sortable: true, formatter: function (value, row) {
                return '<a href="javascript:void(0)" onclick="view_fireLicense_func(\'' + row.id + '\')" class="easyui-linkbutton text-primary" data-options="plain:true">' + value + '</a>';
            }},
            {field: 'fireLevelName', title: '票证级别', width: 60, sortable: true},
            {field: 'content', title: '作业内容', width: 120},
            {field: 'planStartTime', title: '计划开始时间', width: 100, sortable: true},
            {field: 'planEndTime', title: '计划结束时间', width: 100, sortable: true},
            {field: 'auditState', title: '状态', width: 60, sortable: true,formatter:function (value) {
                if (value === 0){
                    return '未执行';
                }
                if (value === 1){
                    return '已执行';
                }
            }},
            {field: 'applyUserName', title: '申请人', width: 60},
            {field: 'applyDeptName', title: '申请部门', width: 100},
            {field: 'addUserName', title: '开票人', width: 60},
            {field: 'addDeptName', title: '开票部门', width: 100},
            {field: 'addTime', title: '开票时间', width: 100, sortable: true}
        ]]
    });
    $('#btn_clear').on("click", function () {
        $('#tb1').form('reset');
        $('#tb2').form('reset').find(':hidden').val('');
    });
    $('#btn_search').on("click", function () {
        $('#dg1').datagrid('reload', $('#tb1,#tb2').find(':input').serializeJson());
    });
    $('#btn_advanced_search').on("click", function () {
        $('#tb2').toggle();
        $('#dg1').datagrid('resize');
    });
    $('#btn_fireLicense_add').on('click', function () {
        add_fireLicense_func();
    });
    $('#btn_fireLicense_edit').on('click', function () {
        edit_fireLicense_func();
    });
    $('#btn_fireLicense_delete').on('click', function () {
        delete_fireLicense_func();
    });
    $('#btn_fireLicense_copy').on('click', function () {
        copy_fireLicense_func();
    });
});